package com.wm.blog_search.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wm.blog_common.result.Result;
import com.wm.blog_config.Document.BlogDocument;
import com.wm.blog_search.service.BlogSearchServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * es controller
 * @author 半卷流年
 * @date 2020-5-22 17:59
 */
@RestController
@Slf4j
@RequestMapping("/blog/es")
public class EsSearchController {

    @Autowired
    private BlogSearchServiceImpl esSearchService;

    /**
     * 新增索引
     * @return
     */
    @PostMapping("/save")
    public Result add(@RequestBody BlogDocument blogDocument) {
        return esSearchService.save(blogDocument);
    }

    /**
     * 删除文档数据
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        return esSearchService.delete(id);
    }
    /**
     * 清空索引
     * @return
     */
    @DeleteMapping("/deleteAll")
    public Result deleteAll() {
        return esSearchService.deleteAll();
    }

    /**
     * 根据ID获取
     * @return
     */
    @GetMapping("/get/{id}")
    public Result<BlogDocument> getById(@PathVariable Long id){
        return esSearchService.getById(id);
    }

    /**
     * 根据获取全部
     * @return
     */
    @GetMapping("/getAll")
    public Result<List<BlogDocument>> getAll(){
        return esSearchService.getAll();
    }

    /**
     * 搜索
     * @param keyword
     * @return
     */
    @GetMapping("query/{keyword}")
    public Result<List<BlogDocument>> query(@PathVariable String keyword){
        List<BlogDocument> list = esSearchService.query(keyword, BlogDocument.class);
        return Result.success(list);
    }

    /**
     * 搜索，命中关键字高亮
     * @param keyword   关键字
     * @param indexName 索引库名称
     * @param fields    搜索字段名称，多个以“，”分割
     * @return
     */
    @GetMapping("queryHit")
    public Result<List<Map<String,Object>>> queryHit(@RequestParam String keyword, @RequestParam String indexName, @RequestParam String fields){
        String[] fieldNames = {};
        if(fields.contains(",")){
            fieldNames = fields.split(",");
        }
        else{
            fieldNames[0] = fields;
        }
        List<Map<String, Object>> list = esSearchService.queryHit(keyword, indexName, fieldNames);
        return Result.success(list);
    }

    /**
     * 搜索，命中关键字高亮
     * http://localhost:8080/query_hit_page?keyword=无印良品荣耀&indexName=orders&fields=productName,productDesc&pageNo=1&pageSize=1
     * @param keyword   关键字
     * @param indexName 索引库名称
     * @param fields    搜索字段名称，多个以“，”分割
     * @return
     */
    @GetMapping("/queryHitPage")
    public Result<Page<Map<String,Object>>> queryHitByPage(@RequestParam int current, @RequestParam int size
            , @RequestParam String keyword, @RequestParam String indexName, @RequestParam String fields){
        String[] fieldNames = {};
        if(fields.contains(",")){
            fieldNames = fields.split(",");
        }
        else{
            fieldNames[0] = fields;
        }
        Page<Map<String, Object>> page = esSearchService.queryHitByPage(current, size, keyword, indexName, fieldNames);
        return Result.success(page);
    }

    /**
     * 删除索引库
     * @param indexName
     * @return
     */
    @DeleteMapping("/deleteIndex/{indexName}")
    public Result<String> deleteIndex(@PathVariable String indexName){
        esSearchService.deleteIndex(indexName);
        return Result.success();
    }

    /**
     * 修改文档数据,局部数据更新,必须保证id存在
     * @param document
     */
    @PutMapping("/updateDocument")
    public Result updateDocument(@RequestBody BlogDocument document) {
        esSearchService.updateDocument(document);
        return Result.success();
    }

    /**
     * 修改文档数据,局部数据更新,必须保证id存在
     * @param id
     */
    @DeleteMapping("/deleteDocument/{id}")
    @Deprecated
    public Result deleteDocument(@PathVariable Long id) {
        esSearchService.deleteDocument(id);
        return Result.success();
    }

}
